/*
 * Date          Author      Note
 * 2015.04.29    wzwz        机头伺服设置
 * 2015.05.19    wzwz        增加伺服类型
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "lang.h"
#include "resource.h"
#include "func.h"
#include "rock_drive.h"
#include "mhbtn.h"
#include "mhlabel.h"
#include "mhctrlstate.h"
#include "com.h"
#include "keypane.h"
#include "info.h"
#include "sys.h"


/*
 *
 */
static void CreateMacDriveInterface(HWND hWnd)
{
    int i = 0;
    const char *sc[8] = {
        GetString(626),
        GetString(627),
        GetString(628),
        GetString(634),
        GetString(635),
        GetString(630),
        GetString(631),
        GetString(774),
    };
    for(i = 0; i < 8; ++i)
    {
        CreateWindow(MH_LABEL_NAME,
            sc[i],
            WS_CHILD | BS_LEFT | WS_VISIBLE,
            IDC_STATIC,
            20,
            60 + i * 30,
            220,
            20,
            hWnd, MH_LABEL_TEXT_GREEN | MH_LABEL_BKGND_BLACK);
    }
    MOTOR_DRIVE motor;
    GetMotorDriveData(g_knit_data, &motor);
    char edit[8][64] = {{0}};
    sprintf(edit[0], "%u", motor.motor_coder);
    sprintf(edit[1], "%u", motor.motor_drive_wheel);
    sprintf(edit[2], "%u", motor.motor_pass_wheel);
    sprintf(edit[3], "%u", motor.motor_belt);
    sprintf(edit[4], "%u", motor.motor_pitch);
    sprintf(edit[5], "%u", motor.motor_elec_molecular);
    sprintf(edit[6], "%u", motor.motor_elec_denominator);
    strcat(edit[7], GetDriverType(g_knit_data) ? GetString(775) : GetString(776));
    for(i = 0; i < 8; ++i)
    {
        CreateWindow(MH_EDIT_NAME,
            edit[i],
            WS_CHILD | WS_VISIBLE,
            IDC_EDIT1 + i,
            240,
            60 + i * 30,
            70,
            20,
            hWnd, 0);
    }
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_YARN_CHANGE_BMP - CX_ASK_BMP * 2,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_FALSE]);
}
/*
 * set edit value
 */
static void OnSetKnitValue(HWND hWnd, ushort nID)
{
    SetWindowAdditionalData(hWnd, TRUE);
}
/*
 *
 */
static void OnChangeDriverType(HWND hWnd, ushort nID)
{
    HWND hTmp = GetDlgItem(hWnd, nID);
    uchar n = GetDriverType(g_knit_data);
    n = !n;
    SetDriverType(g_knit_data, n);
    char sc[64] = {0};
    strcat(sc, n ? GetString(775) : GetString(776));
    SetWindowCaption(hTmp, sc);
    InvalidateRect(hTmp, NULL, TRUE);
    SetWindowAdditionalData(hWnd, TRUE);
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, int nID)
{
    if(nID == ID_ESC)
    {
        if(GetWindowAdditionalData(hWnd))
        {
            MOTOR_DRIVE motor;
            HWND hTmp = GetDlgItem(hWnd, IDC_EDIT1);
            motor.motor_coder = atoi(GetWindowCaption(hTmp));

            hTmp = GetDlgItem(hWnd, IDC_EDIT1 + 1);
            motor.motor_drive_wheel = atoi(GetWindowCaption(hTmp));

            hTmp = GetDlgItem(hWnd, IDC_EDIT1 + 2);
            motor.motor_pass_wheel = atoi(GetWindowCaption(hTmp));

            hTmp = GetDlgItem(hWnd, IDC_EDIT1 + 3);
            motor.motor_belt = atoi(GetWindowCaption(hTmp));

            hTmp = GetDlgItem(hWnd, IDC_EDIT1 + 4);
            motor.motor_pitch = atoi(GetWindowCaption(hTmp));

            hTmp = GetDlgItem(hWnd, IDC_EDIT1 + 5);
            motor.motor_elec_molecular = atoi(GetWindowCaption(hTmp));

            hTmp = GetDlgItem(hWnd, IDC_EDIT1 + 6);
            motor.motor_elec_denominator = atoi(GetWindowCaption(hTmp));

            SetMotorDriveData(g_knit_data, &motor);
            int ret = WriteMacFile(MAC_PARAM_FILE, g_knit_data);
            if(ret)
            {
                printf("write mac drive: %d\n", ret);
                AfxInfoBox(hWnd, NULL, GetString(481));
            }
            else
            {
                MH_FILE_SYNC();
            }
            SendSysParam(g_knit_data);
        }
        PostMessage(hWnd, MSG_CLOSE, 0, 0);
    }
    else if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 7)
    {
        int area[7][2] = {
            {1, 10000},
            {1, 1000},
            {1, 1000},
            {1, 1000},
            {1, 100},
            {1, 100},
            {1, 100},
        };
        int index = nID - IDC_EDIT1;
        HWND hTmp = GetDlgItem(hWnd, nID);
        RECT rect;
        GetClientRect(hTmp, &rect);
        ClientToScreen(hTmp, &rect.left, &rect.top);
        ClientToScreen(hTmp, &rect.right, &rect.bottom);
        CreateKeyboardWindow(hWnd, rect, area[index][0], area[index][1], atoi(GetWindowCaption(hTmp)));
        SetWindowAdditionalData(hWnd, TRUE);
    }
    else if(nID == IDC_EDIT1 + 7)
    {
        //
    }
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    if(key == SCANCODE_ESCAPE)
    {
        OnBtnCmd(hWnd, ID_ESC);
    }
    else if(key == SCANCODE_ENTER)
    {
        ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID == IDC_EDIT1 + 7)
        {
            OnChangeDriverType(hWnd, nID);
        }
        else
        {
            OnBtnCmd(hWnd, nID);
        }
    }
    else if(key == SCANCODE_CURSORBLOCKLEFT)
    {
    }
    else if(key == SCANCODE_CURSORBLOCKRIGHT)
    {
    }
    else if(key == SCANCODE_CURSORBLOCKUP)
    {
        int nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID > IDC_EDIT1 && nID < IDC_EDIT1 + 8)
        {
            --nID;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    else if(key == SCANCODE_CURSORBLOCKDOWN)
    {
        int nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 7)
        {
            ++nID;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
}
/*
 *
 */
static int MacDriveWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateMacDriveInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left += 30;
            rect.top += MASTER_TITLE_TOP;
            rect.right -= 30;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_GREEN(hdc));
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            HWND hHosting = GetHosting(hWnd);
            if(hHosting != HWND_INVALID)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup(hWnd);
        }
        return 0;
    case MSG_KEYDOWN:
        {
            OnKeyDown(hWnd, wParam);
        }
        return 0;
    case MSG_COMMAND:
        {
            OnBtnCmd(hWnd, LOWORD(wParam));
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls(hWnd);
        }
        break;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_YARN_CHANGE]);
        }
        return 0;
    case MSG_SETDATA:
        {
            OnSetKnitValue(hWnd, LOWORD(wParam));
        }
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateMacDriveWindow(HWND hHosting)
{
    HWND hWnd;
    MAINWINCREATE CreateInfo;

    int x = (CX_SCREEN - CX_YARN_CHANGE_BMP) / 2;
    int y = (CY_SCREEN - CY_YARN_CHANGE_BMP) / 2;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = GetString(633);
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = MacDriveWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + CX_YARN_CHANGE_BMP;
    CreateInfo.by = y + CY_YARN_CHANGE_BMP;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = hHosting;
    hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        SwitchToKnitState(g_knit_data, FALSE);
        EnableWindow(hHosting, FALSE);
        SetFocusChild(GetDlgItem(hWnd, IDC_EDIT1));
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    else
    {
    }
    return hWnd;
}
